
package greendroid.widget.item;

import greendroid.widget.itemview.ItemView;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.cyrilmottier.android.greendroid.R;

public class ThumbnailItem extends SubtitleItem {

	/**
	 * The resource ID for the Drawable.
	 */
	public int drawableId;

	/**
	 * An optional URL that may be used to retrieve an image
	 */
	public String drawableURL;

	public OnClickListener onClickListener;

	/**
	 * @hide
	 */
	public ThumbnailItem() {
	}

	/**
	 * Create a new ThumbnailItem.
	 * 
	 * @param text
	 *            The text to draw
	 * @param subtitle
	 *            The subtitle to use
	 * @param drawableId
	 *            The resource identifier to the Drawable
	 */
	public ThumbnailItem(String text, String subtitle, int drawableId, OnClickListener onClickListener) {
		this(text, subtitle, drawableId, null, onClickListener);
		
	}
	
	public ThumbnailItem(String text, String subtitle, int drawableId) {
		this(text, subtitle, drawableId, null, null);
		
	}



	/**
	 * Create a new ThumbnailItem which will asynchronously load the image at
	 * the given URL.
	 * 
	 * @param text
	 *            The text to draw
	 * @param subtitle
	 *            The subtitle to use
	 * @param drawableId
	 *            The default image used when loading the image at the given
	 *            <em>drawableURL</em>
	 * @param drawableURL
	 *            The URL pointing to the image to load.
	 */
	public ThumbnailItem(String text, String subtitle, int drawableId, String drawableURL, OnClickListener onClickListener) {
		super(text, subtitle);
		this.drawableId = drawableId;
		this.drawableURL = drawableURL;
		this.onClickListener = onClickListener;

	}

	@Override
	public ItemView newView(Context context, ViewGroup parent) {
		return createCellFromXml(context, R.layout.gd_thumbnail_item_view, parent);
	}

	@Override
	public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
		super.inflate(r, parser, attrs);

		TypedArray a = r.obtainAttributes(attrs, R.styleable.ThumbnailItem);
		drawableId = a.getResourceId(R.styleable.ThumbnailItem_thumbnail, drawableId);
		drawableURL = a.getString(R.styleable.ThumbnailItem_thumbnailURL);
		a.recycle();
	}

}
